package com.das.mechanic_base.widget;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.intensivetain.X3AloneImageWorkAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.alone.AloneImageAndRecordBean;
import com.das.mechanic_base.bean.custrecord.AloneDetectionBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class X3BottomAloneImageDialog extends X3BaseBottomSheetDialog implements X3AloneImageWorkAdapter.IOnClickAlone {

    RecyclerView rlv_alone;

    private X3AloneImageWorkAdapter workAdapter;
    private List<AloneImageAndRecordBean> imgList = new ArrayList<>();
    private int imageNum;
    private int imageIndex;
    private int titleIndex;
    private boolean isWork;

    public X3BottomAloneImageDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        rlv_alone = getView(R.id.rlv_alone);

        rlv_alone.setLayoutManager(new LinearLayoutManager(mContext));
        workAdapter = new X3AloneImageWorkAdapter(mContext);
        rlv_alone.setAdapter(workAdapter);
        workAdapter.setiOnClickAlone(this);
    }

    public void showCarWork(long carId,int imageNum,int titleIndex,int imageIndex,boolean isWork) {
        this.imageIndex = imageIndex;
        this.imageNum = imageNum;
        this.titleIndex = titleIndex;
        this.isWork = isWork;
        NetWorkHttp.getApi().getDetectionList(carId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView)mContext).bindToLife())
                .subscribe(new HttpCallBack<List<AloneDetectionBean>>() {
                    @Override
                    protected void onDone(List<AloneDetectionBean> aloneDetectionBeans) {
                        showDetection(aloneDetectionBeans);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    private void showDetection(List<AloneDetectionBean> aloneBeans) {
        if (X3StringUtils.isListEmpty(aloneBeans)) {
            aloneBeans = new ArrayList<>();
        }
        if (workAdapter != null) {
            workAdapter.changeAlone(aloneBeans);
        }
    }
    public void setImgList(List<AloneImageAndRecordBean> imgList){
        this.imgList = imgList;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_alone_image_dialog;
    }

    @Override
    public void iOnClickAloneDetail(AloneDetectionBean detectionBean,String title) {
        // 跳转到检测图库
        if (iOnClickAloneImage != null) {
            iOnClickAloneImage.iOnClickAloneImage(detectionBean,imageNum,title,titleIndex,imageIndex,imgList,isWork);
        }
    }

    IOnClickAloneImage iOnClickAloneImage;

    public void setiOnClickAloneImage(IOnClickAloneImage iOnClickAloneImage) {
        this.iOnClickAloneImage = iOnClickAloneImage;
    }

    public interface IOnClickAloneImage {
        void iOnClickAloneImage(AloneDetectionBean detectionBean,int imageNum,String title,int titleIndex,int imageIndex,List<AloneImageAndRecordBean> imgList,boolean isWork);
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("施工工艺检测报告图库弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("施工工艺检测报告图库弹窗");
    }
}
